<?php
class CodeSpace_Algorithm_MPTT
{

    public static function findParentChain (
        Zend_Db_Table_Row_Abstract $row, $includeItself = false)
    {
        $tableName = $row->getTable();
        $tableClassName = $row->getTableClass();
        $prefix = $row->_prefix;
        $leftColumnName = $prefix . '_left';
        $rightColumnName = $prefix . '_right';
        $leftValue = $row->getLeftValue();
        $rightValue = $row->getRightValue();
        $table = new $tableClassName();
        if (! $includeItself)
        {
            $q = $table->select()
                ->where($leftColumnName . ' < ?', $leftValue)
                ->where($rightColumnName . ' > ? ', $rightValue);
        }
        else
        {
            $q = $table->select()
                ->where($leftColumnName . ' <= ?', $leftValue)
                ->where($rightColumnName . ' >= ? ', $rightValue);
        }
        return $table->fetchAll($q);
    }

    public static function findChildChain (
        Zend_Db_Table_Row_Abstract $row, $includeItself = false)
    {
        $tableName = $row->getTable();
        $tableClassName = $row->getTableClass();
        $prefix = $row->_prefix;
        $leftColumnName = $prefix . '_left';
        $rightColumnName = $prefix . '_right';
        $leftValue = $row->getLeftValue();
        $rightValue = $row->getRightValue();
        $table = new $tableClassName();
        if (! $includeItself)
        {
            $q = $table->select()
                ->where($leftColumnName . ' > ?', $leftValue)
                ->where($rightColumnName . ' < ? ', $rightValue);
        }
        else
        {
            $q = $table->select()
                ->where($leftColumnName . ' >= ?', $leftValue)
                ->where($rightColumnName . ' <= ? ', $rightValue);
        }
        return $table->fetchAll($q);
    }

    public static function addAsChildOf (Zend_Db_Table_Row_Abstract $row)
    {
        // Retrive the temp left-right value
    // Increase all index by 2
    // Add $row
    }

    public static function delete (Zend_Db_Table_Row_Abstract $row)
    {
        // Retrive the temp left-right value
    // Delete $row
    // Decsease all index by 2
    }

    public static function moveToChildOf (Zend_Db_Table_Row_Abstract $row)
    {
        // Delete First
        self::delete($row);
        // Add as Child of then
        self::addAsChildOf($row);
    }
}
?>